package cn.com.yusys.yusp.mobile.service;

import cn.com.yusys.yusp.admin.Utils.Clob2StringUtils;
import cn.com.yusys.yusp.admin.Utils.IDUtils;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;
import cn.com.yusys.yusp.mobile.repository.mapper.WcrmGridInfoMapper;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author
 * @since 2021-06-24 10:24:09
 */
@Service("wcrmGridInfoService")
public class WcrmGridInfoService extends CommonService {

    private static Logger logger = LoggerFactory.getLogger(WcrmGridInfoService.class);
    @Resource
    private WcrmGridInfoMapper wcrmGridInfoMapper;


    @Override
    protected CommonMapper getMapper() {
        return null;
    }

//    public List<Map<String, Object>> selectGrid(QueryModel queryModel) {
//        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
//        List<Map<String, Object>> list = null;
//        //角色Id  机构Id
//        String role = queryModel.getCondition().get("role").toString();
//        if("R0008".equals(role)||"R0007".equals(role)){//总行
//            list = wcrmGridInfoMapper.selectGridInfoZH(queryModel);
//        }else if("R0005".equals(role) || "R0003".equals(role) || "R0004".equals(role)){ //行长
//            list = wcrmGridInfoMapper.selectGridInfoHZ(queryModel);
//        }else if("R0002".equals(role) || "R0001".equals(role) || "R0009".equals(role)){ // 客户经理
//            list = wcrmGridInfoMapper.selectGridInfoKUJL(queryModel);
//        }else{
//        }
//        PageHelper.clearPage();
//        return list;
//    }
//
//
//    public List<Map<String, Object>> selectGridInfo(QueryModel queryModel) {
//        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
//        List<Map<String, Object>> list = null;
//        //角色Id  机构Id
//        String role = queryModel.getCondition().get("role").toString();
//        if("R0008".equals(role)||"R0007".equals(role)){//总行
//            queryModel.getCondition().put("statisType","01");
//        } else{
//            queryModel.getCondition().put("statisType","03");
//        }
//        list = wcrmGridInfoMapper.selectGridInfo(queryModel);
//        PageHelper.clearPage();
//        return list;
//    }

    public List<Map<String, Object>> selectGridInfoZH(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoZH(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> selectGridInfoZHH(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoZHH(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> selectGridInfoKUJL(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoKUJL(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> selectGridInfoFH(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoFH(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> selectGridInfoTwo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoTwo(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> selectGridInfoKeyMan(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfoKeyMan(queryModel);
        PageHelper.clearPage();
        return list;
    }

    public List<Map<String, Object>> gridCustInfoLevelSelectTwo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.gridCustInfoLevelSelectTwo(queryModel);
        PageHelper.clearPage();
        return map2MapStirngId(list);
    }
    public List<Map<String, Object>> gridMerchantCustInfoSelect(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.gridMerchantCustInfoSelect(queryModel);
        PageHelper.clearPage();
        return map2MapStirngId(list);
    }
    public List<Map<String, Object>> transformationSelect(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.transformationSelect(queryModel);
        PageHelper.clearPage();
        return list;
    }
    public List<Map<String, Object>> actInfoSelect(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        String roleId = (String) queryModel.getCondition().get("roleId");
        String orgId = (String) queryModel.getCondition().get("orgId");
        if (orgId.equals("070667XN0") || orgId.equals("070667XN1") || orgId.equals("070667860") || orgId.equals("070667XN3")) {
            queryModel.getCondition().put("roleLevel", "1");
            list = wcrmGridInfoMapper.actInfoSelect(queryModel);
            return list;
        }
        //查询支行下所属的所有客户经理的用户 Sub-branch Manager
        //客户经理
        if (roleId.equals("R0001") || roleId.equals("R0002") || roleId.equals("R0009")) {
            queryModel.getCondition().put("roleLevel", "2");
            list = wcrmGridInfoMapper.actInfoSelect(queryModel);
        }
        //查询所有用户
        //总行管理员
        if (roleId.equals("R0008") || roleId.equals("R0007") || roleId.equals("R001")) {
            queryModel.getCondition().put("roleLevel", "0");
            list = wcrmGridInfoMapper.actInfoSelect(queryModel);
        }
        //支行行长查询，显示其机构下所有客户
        if (roleId.equals("R0006") || roleId.equals("R0005") || roleId.equals("R0003") || roleId.equals("R0004")) {
            queryModel.getCondition().put("roleLevel", "2");
            list = wcrmGridInfoMapper.actInfoSelect(queryModel);
        }
        PageHelper.clearPage();
        return list;
    }


    public List<Map<String, Object>> queryActInfo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.queryActInfo(queryModel);
        PageHelper.clearPage();
        return list;
    }
    public List<Map<String, Object>> queryGridMgr(QueryModel queryModel) {
//        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.queryGridMgr(queryModel);
//        PageHelper.clearPage();
        return map2MapStirngId(list);
    }
    public List<Map<String, Object>> queryGridSumInfo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.queryGridSumInfo(queryModel);
        PageHelper.clearPage();
        return list;
    }
    public List<Map<String, Object>> transformationSelectOneLevelGrid(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.transformationSelectOneLevelGrid(queryModel);
        PageHelper.clearPage();
        return list;
    }


    public List<Map<String, Object>> queryOnelevelGridMgr(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.queryOnelevelGridMgr(queryModel);
        PageHelper.clearPage();
        return list;
    }
    public List<Map<String, Object>> queryGridMgrInfo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.queryGridMgrInfo(queryModel);
        PageHelper.clearPage();
        return map2MapStirngGridId(list);
    }
    public List<Map<String, Object>> transformationSelectThirdLevelGrid(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.transformationSelectThirdLevelGrid(queryModel);
        PageHelper.clearPage();
        return list;
    }
    public List<Map<String, Object>> transformationSelectTwoLevelGrid(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.transformationSelectTwoLevelGrid(queryModel);
        PageHelper.clearPage();
        return list;
    }


    public List<Map<String, Object>> selectGridInfo(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.selectGridInfo(queryModel);
        PageHelper.clearPage();
        return map2MapStirngTableId(list);
    }

    public List<Map<String, Object>> gridCustInfoLevelSelect(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.gridCustInfoLevelSelect(queryModel);
        PageHelper.clearPage();
        return map2MapStirngId(list);
    }

    public List<Map<String, Object>> gridCustInfoLevelSelectZH(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        List<Map<String, Object>> list = null;
        list = wcrmGridInfoMapper.gridCustInfoLevelSelectZH(queryModel);
        PageHelper.clearPage();
        return map2MapStirngId(list);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Integer saveKeymanInfo(Map map) {
        int i = 0;
        int i1 = 0;
        if(map.get("gridId") == null || "".equals(map.get("gridId"))){

        }else{
            map.put("lastUpdateTime", DateUtil.formatDateTimeByDef());
            i = wcrmGridInfoMapper.updateGridTypeById(map);
            List<Map<String, Object>> list = wcrmGridInfoMapper.selectGridDetailByGridId(map);
            if(list.size()>0){
                i1 = wcrmGridInfoMapper.updateGridInfoById(map);
            }else{
                map.put("id", IDUtils.getGuid());
                map.put("createUser", map.get("lastUpdateUser"));
                map.put("createTime", DateUtil.formatDateTimeByDef());
                map.put("createUserName", map.get("lastUpdateUserName"));
                i1 = wcrmGridInfoMapper.insertGridInfoById(map);
            }
        }
        return i+i1;
    }
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Integer keymanInfoModify(Map queryModel) {
        int i = 0;
        if (queryModel.get("keymanId") == null || "".equals(queryModel.get("keymanId"))){
            queryModel.put("id",IDUtils.getGuid());
            queryModel.put("createUser", queryModel.get("lastUpdateUser"));
            queryModel.put("createTime", DateUtil.formatDateTimeByDef());
            queryModel.put("createUserName", queryModel.get("lastUpdateUserName"));
            i = wcrmGridInfoMapper.insertKeyManGridData(queryModel);
        }else{
            queryModel.put("id",queryModel.get("keymanId"));
            queryModel.put("lastUpdateTime", DateUtil.formatDateTimeByDef());
            i = wcrmGridInfoMapper.updateKeyManGridData(queryModel);
        }
        return i;
    }


    /**
     * 批量删除关键人的业务逻辑
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public int deleteBatch(String ids) {
        return wcrmGridInfoMapper.deleteById(ids);
    }

    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    public Integer saveActPicInfo(Map queryModel) {
        int i = 0;
        queryModel.put("lastUpdateTime", DateUtil.formatDateTimeByDef());
        i=wcrmGridInfoMapper.saveActPicInfo(queryModel);
        return i;
    }


    //Map value clob -> Map value String
    public List<Map<String, Object>> map2MapStirngTableId(List<Map<String, Object>> maps) {
        Map<String, String> markLatitude = new HashMap<>();
        Map<String, String> markLongitude = new HashMap<>();
        maps.stream().forEach(item -> {
            try {
                markLatitude.put((String) item.get("tableId"), Clob2StringUtils.ClobToString((Clob) item.get("latitude")));
                markLongitude.put((String) item.get("tableId"), Clob2StringUtils.ClobToString((Clob) item.get("longitude")));
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        for (int i = 0; i < maps.size(); i++) {
            for (Map.Entry<String, String> entry : markLatitude.entrySet()) {
                if (entry.getKey().equals(maps.get(i).get("tableId").toString())) {
                    maps.get(i).put("latitude", entry.getValue());
                }
            }
            for (Map.Entry<String, String> entry1 : markLongitude.entrySet()) {
                if (entry1.getKey().equals(maps.get(i).get("tableId").toString())) {
                    maps.get(i).put("longitude", entry1.getValue());
                }
            }
        }
        return maps;
    }

    //Map value clob -> Map value String
    public List<Map<String, Object>> map2MapStirngId(List<Map<String, Object>> maps) {
        Map<String, String> markLatitude = new HashMap<>();
        Map<String, String> markLongitude = new HashMap<>();
        maps.stream().forEach(item -> {
            try {
                markLatitude.put((String) item.get("id"), Clob2StringUtils.ClobToString((Clob) item.get("latitude")));
                markLongitude.put((String) item.get("id"), Clob2StringUtils.ClobToString((Clob) item.get("longitude")));
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        for (int i = 0; i < maps.size(); i++) {
            for (Map.Entry<String, String> entry : markLatitude.entrySet()) {
                if (entry.getKey().equals(maps.get(i).get("id").toString())) {
                    maps.get(i).put("latitude", entry.getValue());
                }
            }
            for (Map.Entry<String, String> entry1 : markLongitude.entrySet()) {
                if (entry1.getKey().equals(maps.get(i).get("id").toString())) {
                    maps.get(i).put("longitude", entry1.getValue());
                }
            }
        }
        return maps;
    }

    //Map value clob -> Map value String
    public List<Map<String, Object>> map2MapStirngGridId(List<Map<String, Object>> maps) {
        Map<String, String> markLatitude = new HashMap<>();
        Map<String, String> markLongitude = new HashMap<>();
        maps.stream().forEach(item -> {
            try {
                markLatitude.put((String) item.get("gridId"), Clob2StringUtils.ClobToString((Clob) item.get("latitude")));
                markLongitude.put((String) item.get("gridId"), Clob2StringUtils.ClobToString((Clob) item.get("longitude")));
            } catch (SQLException e) {
                e.printStackTrace();

            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        for (int i = 0; i < maps.size(); i++) {
            for (Map.Entry<String, String> entry : markLatitude.entrySet()) {
                if (entry.getKey().equals(maps.get(i).get("gridId").toString())) {
                    maps.get(i).put("latitude", entry.getValue());
                }
            }
            for (Map.Entry<String, String> entry1 : markLongitude.entrySet()) {
                if (entry1.getKey().equals(maps.get(i).get("gridId").toString())) {
                    maps.get(i).put("longitude", entry1.getValue());
                }
            }
        }
        return maps;
    }
}